home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 November / Chip Kasım 2000.iso / prog / basic / 09 / AXA2.CAB / DAJAVA.CAB / com / ms / dxmedia / DXMCanvasBase.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-10-14  |  6.5 KB  |  302 lines

  1. package com.ms.dxmedia;
  2.  
  3. import com.ms.awt.HeavyComponent;
  4. import com.ms.awt.WComponentPeer;
  5. import com.ms.com.ComException;
  6. import com.ms.com.ComFailException;
  7. import com.ms.dxmedia.rawcom.DAView;
  8. import com.ms.dxmedia.rawcom.IDAPreferences;
  9. import com.ms.dxmedia.rawcom.IDAView;
  10. import com.ms.dxmedia.rawcom.IDAViewSite;
  11. import java.applet.Applet;
  12. import java.awt.Canvas;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Graphics;
  16. import java.awt.event.ComponentEvent;
  17. import java.awt.event.ComponentListener;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20.  
  21. public abstract class DXMCanvasBase extends Canvas implements IDAViewSite, Viewer, HeavyComponent, ComponentListener {
  22.    private IDAView _view;
  23.    private ViewEventCB _eventCB;
  24.    private Preferences _prefs;
  25.    private Model _model;
  26.    private long _startTime;
  27.    private double _lastTick;
  28.    private long _minFrameTime;
  29.    ErrorAndWarningReceiver _errorRecv = new DefaultErrReceiver();
  30.  
  31.    public synchronized void startModel() throws DXMException {
  32.       try {
  33.          if (this._model == null) {
  34.             throw this.handleError(-2147467259, "No model set", false);
  35.          } else {
  36.             this.ensureViewIsCreated();
  37.             this._view.putWindow(((WComponentPeer)((Component)this).getPeer()).gethwnd());
  38.             this._view.putSite(this);
  39.             this._lastTick = (double)0.0F;
  40.             if (this._model.getImportBase() == null) {
  41.                this._model.setImportBase(this.calculateDocBase());
  42.             }
  43.  
  44.             BvrsToRun var1 = new BvrsToRun(this._view);
  45.             this._model.createModel(var1);
  46.             var1.invalidate();
  47.             ImageBvr var2 = this._model.getImage();
  48.             if (var2 == null) {
  49.                var2 = Statics.emptyImage;
  50.             }
  51.  
  52.             SoundBvr var3 = this._model.getSound();
  53.             if (var3 == null) {
  54.                var3 = Statics.silence;
  55.             }
  56.  
  57.             this._view.StartModel(var2.getCOMPtr(), var3.getCOMPtr(), (double)0.0F);
  58.             this._model.setImage((ImageBvr)null);
  59.             this._model.setSound((SoundBvr)null);
  60.             this._startTime = System.currentTimeMillis();
  61.             this._eventCB = new ViewEventCB(this._view, this, this, this._startTime);
  62.             Preferences var4 = this.getPreferences();
  63.             long var5 = (long)var4.getDouble("Max FPS");
  64.             if (var5 > 0L) {
  65.                this._minFrameTime = 1000L / var5;
  66.             } else {
  67.                this._minFrameTime = 0L;
  68.             }
  69.  
  70.          }
  71.       } catch (ComFailException var8) {
  72.          throw this.handleError(var8, false);
  73.       }
  74.    }
  75.  
  76.    public synchronized void stopModel() throws DXMException {
  77.       try {
  78.          if (this._view != null) {
  79.             this._view.StopModel();
  80.             this._view = null;
  81.          }
  82.  
  83.          this._eventCB = null;
  84.          this._startTime = 0L;
  85.          this._lastTick = (double)0.0F;
  86.       } catch (ComFailException var3) {
  87.          throw this.handleError(var3, false);
  88.       }
  89.    }
  90.  
  91.    public abstract synchronized void showModel();
  92.  
  93.    public void ReportError(int var1, String var2) {
  94.    }
  95.  
  96.    public void componentShown(ComponentEvent var1) {
  97.       this.showModel();
  98.    }
  99.  
  100.    private URL calculateDocBase() {
  101.       boolean var1 = false;
  102.       Container var2 = ((Component)this).getParent();
  103.  
  104.       while(var2 != null && !var1) {
  105.          if (var2 instanceof Applet) {
  106.             var1 = true;
  107.          } else {
  108.             var2 = ((Component)var2).getParent();
  109.          }
  110.       }
  111.  
  112.       URL var3;
  113.       if (var1) {
  114.          Applet var4 = (Applet)var2;
  115.          var3 = var4.getCodeBase();
  116.       } else {
  117.          try {
  118.             String var7 = "file:/" + System.getProperty("user.dir") + "/";
  119.             var7 = var7.replace('\\', '/');
  120.             var3 = new URL(var7);
  121.          } catch (MalformedURLException var6) {
  122.             throw this.handleError(3, ((Throwable)var6).toString(), false);
  123.          }
  124.       }
  125.  
  126.       return var3;
  127.    }
  128.  
  129.    protected synchronized double systemTimeToGlobalTime(long var1) {
  130.       return (double)(var1 - this._startTime) / (double)1000.0F;
  131.    }
  132.  
  133.    public void componentResized(ComponentEvent var1) {
  134.    }
  135.  
  136.    public void componentHidden(ComponentEvent var1) {
  137.       this.hideModel();
  138.    }
  139.  
  140.    protected synchronized ViewEventCB getEventCB() {
  141.       return this._eventCB;
  142.    }
  143.  
  144.    public synchronized double getCurrentTickTime() {
  145.       return this._lastTick;
  146.    }
  147.  
  148.    public void update(Graphics var1) {
  149.       this.paint(var1);
  150.    }
  151.  
  152.    public abstract synchronized void addModel();
  153.  
  154.    public synchronized ErrorAndWarningReceiver getErrorAndWarningReceiver() {
  155.       return this._errorRecv;
  156.    }
  157.  
  158.    protected synchronized long getStartTime() {
  159.       return this._startTime;
  160.    }
  161.  
  162.    public boolean needsHeavyPeer() {
  163.       return true;
  164.    }
  165.  
  166.    public synchronized void removeNotify() {
  167.       this.removeModel();
  168.       super.removeNotify();
  169.    }
  170.  
  171.    public void SetStatusText(String var1) {
  172.       System.out.println(var1);
  173.    }
  174.  
  175.    public abstract synchronized void removeModel();
  176.  
  177.    public abstract synchronized void hideModel();
  178.  
  179.    public synchronized void paint(Graphics var1) {
  180.       if (this._eventCB != null) {
  181.          this._eventCB.RePaint(var1);
  182.       }
  183.  
  184.    }
  185.  
  186.    public synchronized void addNotify() {
  187.       super.addNotify();
  188.       this.addModel();
  189.    }
  190.  
  191.    public synchronized ErrorAndWarningReceiver registerErrorAndWarningReceiver(ErrorAndWarningReceiver var1) {
  192.       ErrorAndWarningReceiver var2 = this._errorRecv;
  193.       this._errorRecv = var1;
  194.       return var2;
  195.    }
  196.  
  197.    public void cleanup() {
  198.       this.stopModel();
  199.       if (this._model != null) {
  200.          this._model.cleanup();
  201.          this._model = null;
  202.       }
  203.  
  204.       this._errorRecv = null;
  205.       this._prefs = null;
  206.    }
  207.  
  208.    public synchronized void tick(double var1) throws DXMException {
  209.       try {
  210.          if (this._view != null) {
  211.             this._lastTick = var1;
  212.             long var3 = System.currentTimeMillis();
  213.             if (this._view.Tick(var1)) {
  214.                this._view.Render();
  215.             }
  216.  
  217.             long var5 = System.currentTimeMillis() - var3;
  218.             if (var5 < this._minFrameTime) {
  219.                try {
  220.                   Thread.currentThread();
  221.                   Thread.sleep(this._minFrameTime - var5);
  222.                } catch (InterruptedException var8) {
  223.                }
  224.             }
  225.          }
  226.  
  227.       } catch (ComFailException var9) {
  228.          throw this.handleError(var9, true);
  229.       }
  230.    }
  231.  
  232.    public void tick() throws DXMException {
  233.       this.tick(this.getCurrentTime());
  234.    }
  235.  
  236.    public DXMCanvasBase() {
  237.       ((Component)this).addComponentListener(this);
  238.    }
  239.  
  240.    protected synchronized IDAView getView() {
  241.       return this._view;
  242.    }
  243.  
  244.    public synchronized Preferences getPreferences() throws DXMException {
  245.       if (this._prefs == null) {
  246.          try {
  247.             this.ensureViewIsCreated();
  248.             IDAPreferences var1 = this._view.getPreferences();
  249.             this._prefs = new Preferences(var1);
  250.          } catch (ComFailException var3) {
  251.             throw this.handleError(var3, false);
  252.          }
  253.       }
  254.  
  255.       return this._prefs;
  256.    }
  257.  
  258.    public void componentMoved(ComponentEvent var1) {
  259.    }
  260.  
  261.    protected synchronized void ensureViewIsCreated() throws DXMException {
  262.       if (this._view == null) {
  263.          this._view = new DAView();
  264.       }
  265.  
  266.    }
  267.  
  268.    public synchronized void clearModel() {
  269.       this._model = null;
  270.    }
  271.  
  272.    protected DXMException handleError(ComFailException var1, boolean var2) throws DXMException {
  273.       return this.handleError(((ComException)var1).getHResult(), ((Throwable)var1).getMessage(), var2);
  274.    }
  275.  
  276.    protected DXMException handleError(int var1, String var2, boolean var3) throws DXMException {
  277.       if (var3) {
  278.          this._errorRecv.handleTickError(var1, var2, this);
  279.       } else {
  280.          this._errorRecv.handleError(var1, var2, this);
  281.       }
  282.  
  283.       return new DXMException(var1, var2);
  284.    }
  285.  
  286.    public synchronized Model getModel() {
  287.       return this._model;
  288.    }
  289.  
  290.    public synchronized void setModel(Model var1) throws DXMException {
  291.       if (this._model == null) {
  292.          this._model = var1;
  293.       } else {
  294.          throw this.handleError(-2147467259, "setModel: Model already set", false);
  295.       }
  296.    }
  297.  
  298.    public double getCurrentTime() {
  299.       return this.systemTimeToGlobalTime(System.currentTimeMillis());
  300.    }
  301. }
  302.